package com.showcai.controller.refundApply;

import com.showcai.controller.base.WeChatBaseController;
import com.showcai.module.ApplyWatch;
import com.showcai.module.RefundApply;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.ApplyService;
import com.showcai.service.OrderService;
import com.showcai.service.RefundApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Baggio on 2018-1-25.
 */
@RequestMapping("/refund")
@Controller
public class RefundApplyController extends WeChatBaseController {
    @Autowired
    RefundApplyService refundApplyService;
    @Autowired
    ApplyService applyWatchService;
    @Autowired
    OrderService orderService;
 /**
     * 退款申请
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/refundApplyPage")
    public ModelAndView refundApplyPage(Long orderId) {
        ApplyWatch applyWatch = refundApplyService.selectRefundApply(orderId);
        ModelAndView mov = new ModelAndView();
        //跳转路径
        String views = "refund/web/refundApplyPage";
        if (getIsMobile()) {
            views = "refund/wap/refundApplyPage";
        }
        mov.setViewName(views);
        mov.addObject("applyWatch",applyWatch);
        return mov;
    }
    /**
     * 退款申请成功
     * @param refundApply
     * @return
     */
    @RequestMapping(value = "/refundApply")
    @ResponseBody
    public String refundApply(RefundApply refundApply) {
        ResultObj resultObj = new ResultObj();
        try {
            ApplyWatch applyWatch = (ApplyWatch)applyWatchService.selectApplyByOrderId(refundApply.getOrderId());
            applyWatch.setStatus(OrderService.REFUND);
            refundApplyService.addRefundApply(refundApply);
            applyWatchService.modifyApply(applyWatch);
            orderService.modifyOrderStatus(refundApply.getOrderId(),OrderService.REFUND);
            resultObj.setData(refundApply);
        } catch (MyRunTimeExcption e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(ReturnCode.FAIL.getMsg());
            e.printStackTrace();
        }catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(ReturnCode.FAIL.getMsg());
            e.printStackTrace();
        }

        return resultObj.toJson();
    }
    @RequestMapping(value = "/refundApplySuccess")
    public ModelAndView refundApplySuccess() {
        ModelAndView mov = new ModelAndView();

        //跳转路径
        String views = "refund/web/refundApplySuccess";
        if (getIsMobile()) {
            views = "refund/wap/refundApplySuccess";
        }
        mov.setViewName(views);
        return mov;

    }

}
